#coding=utf-8
__author__ = 'Administrator'

import wx
import wx.lib.filebrowsebutton as filebrowse
import convert
import tkMessageBox
import os

radioArr = {"unicode => ascii" : 0, "ascii => unicode" : 1}

class InsertFrame(wx.Frame):
    def __init__(self, parent, id=-1):
        wx.Frame.__init__(self, parent, id, title=u"文件格式转换")
        panel = wx.Panel(self) #创建画板

        wx.static = wx.StaticText(panel, -1,
                                  pos=(50, 150),
                                  label=""#, style=wx.ST_DOTS_MIDDLE
        )

        self.radio = wx.RadioBox(panel, id,
                    pos = (40, 40),
                    label="", choices=radioArr.keys())
        self.btnConvert = wx.Button(panel, id, label=u'转换', pos=(50, 100))
        self.btnClose = wx.Button(panel, id, label=u'取消', pos=(200, 100))
        self.Bind(wx.EVT_BUTTON, self.OnConvert, self.btnConvert)
        self.Bind(wx.EVT_BUTTON, self.OnClose, self.btnClose)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        self.Bind(wx.EVT_RADIOBOX, self.EvtRadioBox, self.radio)

        self.fbbIn = filebrowse.FileBrowseButtonWithHistory(
            panel, -1, size=(350, -1), pos=(10, 10)
            , changeCallback = self.fbbinCallback
            , labelText=u'文件路径'
            , buttonText='...'
            , dialogTitle=u'打开文件'
        )
    def fbbinCallback(self, event):
        if hasattr(self, 'fbbIn'):
            value = event.GetString()
            if not value:
                return
#            self.log.write('FileBrowseButtonWithHistory: %s\n' % value)
            history = self.fbbIn.GetHistory()
            if value not in history:
                history.append(value)
                self.fbbIn.SetHistory(history)
                self.fbbIn.GetHistoryControl().SetStringSelection(value)
    def EvtRadioBox(self, event):
        print event.GetInt()
    def OnConvert(self, event):
        ret = 0
        filePath = self.fbbIn.GetValue()
        radioString = self.radio.GetStringSelection()
        if filePath == u"":
            self.ShowMessageBox(u"请输入文件路径")
            return
        if radioString == "unicode => ascii":
            if os.path.isfile( filePath ) and -1 == convert.checkfile(filePath, 'utf-16'):
                self.ShowMessageBox(u"文件格式有误")
                return
            filelist = convert.ConverFile(filePath, 'utf-16', 'gb18030')
        elif radioString == "ascii => unicode":
            if os.path.isfile( filePath ) and -1 == convert.checkfile(filePath, 'gb18030'):
                self.ShowMessageBox(u"文件格式有误")
                return
            filelist = convert.ConverFile(filePath, 'gb18030', 'utf-16')

        s = u'转换完毕!\r\n'
        for file in filelist:
            s += file
            s += '\r\n'
        self.ShowMessageBox(s)

    def ShowMessageBox(self, mess, title=u"警告", style=wx.OK | wx.ICON_WARNING):
        dlg = wx.MessageDialog(self,mess, title, style)
        dlg.ShowModal()
        dlg.Destroy()
    def OnClose(self, evnet):
        self.Close(True)
    def OnCloseWindow(self, event):
        self.Destroy()


if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = InsertFrame(None)
    frame.Show()
    app.MainLoop()

  